/*
 * Event queue manipulations header file
 *
 * Copyright (C) 2020 Long-Horn.
 *
 */
#ifndef EVENT_QUEUE
#define EVENT_QUEUE

#include <stdint.h>
#include <pthread.h>

#include "event.h"

#ifdef __cplusplus
extern "C"  {
#endif

/** Blocking event  queue data structure
 */
typedef struct __event_queue {
    // interface functions
    int (* enqueue)(void *q, event_s *event);
    size_t (* dequeue)(void *q, event_s *event);
    
    // backing buffer and size
    unsigned char *buffer;
    size_t size;
    
    // read / write indices
    size_t head;
    size_t tail;

    // synchronization primitives
    pthread_cond_t readable;
    pthread_cond_t writeable;
    pthread_mutex_t mutex;
} event_queue_s;

/** Initialize a blocking queue *q* of size *n*
 */
int create_event_queue(event_queue_s **event_q, size_t n);
void destroy_event_queue(event_queue_s *q);

#ifdef __cplusplus
}
#endif

#endif
